<template>
  <div>
    <div>
      <ul>
        <li class="img-box" :key="index" v-for="(item,index) in imgList">
          <img :src="item" v-preview="item" style="width:100%; height:100%;" alt="">
          <div class="del">
            <img @click="delImage(index)" src="../assets/icon/del1.png" alt="">
          </div>
        </li>
        <li class="add-icon" @click="upload">
          <input type="file" ref="uplod_put" @change="uploadImage($event)" multiple style="display:none;">
          <img src="../assets/icon/add.png" style="width:30px;height:30px;" alt="">
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        imgList: [], // 图片
      }
    },
    methods: {
      // 触发上传
      upload() {
        this.$refs.uplod_put.click()
      },
      //上传并回显图片
      uploadImage(e) {
        let _this = this
        let img_file = e.target.files
        console.log(img_file)
        for (let i = 0; i < img_file.length; i++) {
          let reader = new FileReader()
          reader.readAsDataURL(img_file[i])
          reader.onload = function (e) {
            _this.imgList.push(e.target.result)
          }
        }
      },
      // 删除图片
      delImage(index) {
        this.$delete(this.imgList, index)
      }
    }

  }

</script>
<style scoped>
  ul {
    display: flex;
    padding: 0;
    margin: 30px 0 0 0;
    flex-wrap: wrap;
  }

  ul li {
    list-style-type: none;
    width: calc((100% - 23px) / 4);
    border: 1px solid #d9d9d9;
    overflow: hidden;
    margin-bottom: 20px;
    border-radius: 5px;
    margin-right: 5px;
    height: 100px;
    line-height: 0;
  }

  ul li:nth-child(4n+0) {
    margin-right: 0;
  }

  .add-icon {
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    overflow: hidden;
  }

  .img-box .del {
    position: absolute;
    width: calc((100% - 41px) / 4);
    border-radius: 0 0 5px 5px;
    text-align: right;
    /* background: #a59a9a6b; */
    margin-top: -25px;
    margin-right: 10px;
  }

  .img-box .del img {
    width: 25px;
    height: 25px;
  }

</style>
